package com.hyc.myvideo.ui.activity.my;

import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.commission.CommissionDetailsAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.ModelCommission;
import com.hyc.myvideo.ui.base.BaseFActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 佣金详情
 * @author gyb
 * @version 1.0
 * @created 2017/07/20
 */
public class CommissionDetailsActivity extends BaseFActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.common_head_list_id)
    PullToRefreshListView toRefreshListView;
    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;
    private CommissionDetailsAdapter adapter = null;
    private List<ModelCommission> ListData = null;
    @Override
    public int getContentViewId() {
        return R.layout.activity_common_listview_head;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true, getIntent().getStringExtra("title"));
        toRefreshListView.setOnItemClickListener(this);
        /**设置间距*/
        ListView listView= toRefreshListView.getRefreshableView();
        listView.setDivider(new ColorDrawable(res.getColor(R.color.color_0)));
        listView.setDividerHeight(15);
        ListData = new ArrayList<>();
        adapter = new CommissionDetailsAdapter(this, ListData);
        toRefreshListView.setAdapter(adapter);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestRegister(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestRegister(false);
            }
        });
    }

    @Override
    protected void initData() {
        requestRegister(true);
    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<List<ModelCommission>> result = JSONObject.parseObject(r, new TypeReference<BaseResult<List<ModelCommission>>>(){});
        if(result.isCode()){
            if(isRefresh){
                ListData.clear();
            }
            ListData.addAll(result.getResult());
            adapter.updateListView(ListData);
        }
        clearedListView();
    }

    @Override
    protected void oError() {
        clearedListView();
    }

    /***
     * 请求数据
     * @param isRefresh 是否刷新
     */
    private void requestRegister(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestCommissionDeiails(page);
    }

    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int size = toRefreshListView.getRefreshableView().getHeaderViewsCount();

    }
}
